﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;

namespace PVFSLEAP
{
	public partial class F_NDLG: Form
	{
		C_MSSG MG = new C_MSSG();
		C_COMM CM = new C_COMM();

		string FileName		= "SimulationResult";
		string DefaultExt	= "xls";
		string Filter		= "Excel files (*.xls)|*.xls";
		string Directory	= "C:\\Users\\SMNAM\\Desktop";

		string TFRI;
		string TTFR;

		public F_NDLG()
		{
			InitializeComponent();
		}

		public F_NDLG( int ndct, int[] ndid, int[] prid, int[] hpct, double[] engy, float[] cost, int[] ovri, int[] ofra, int[] owrm, int[] dfva, int[] dfra, int[] dwrm )
		{
			InitializeComponent();

			for ( int i = 0; i < ndct; i++ )
			{
				DG_NDLG.Rows.Add( 1 );

				DG_NDLG.Rows[i].HeaderCell.Value = ( i + 1 ).ToString();

				DG_NDLG[ 0 ,i].Value = ndid[i];
				DG_NDLG[ 1, i].Value = prid[i];
				DG_NDLG[ 2, i].Value = hpct[i];
				DG_NDLG[ 3, i].Value = engy[i];
				DG_NDLG[ 4, i].Value = cost[i];
				DG_NDLG[ 5, i].Value = ovri[i];
				DG_NDLG[ 6, i].Value = ofra[i];
				DG_NDLG[ 7, i].Value = owrm[i];

				DG_NDLG[ 8, i].Value = dfva[i];
				DG_NDLG[ 9, i].Value = dfra[i];
				DG_NDLG[10, i].Value = dwrm[i];
				//DG_NDLG[10, i].Value = dfrc[i];
				//DG_NDLG[11, i].Value = dfmc[i];

				//DG_NDLG[12, i].Value = spid[i];
				//DG_NDLG[13, i].Value = skey[i];

				//DG_NDLG[14, i].Value = indk[i];
				//DG_NDLG[15, i].Value = prwk[i];
				//DG_NDLG[16, i].Value = clsk[i];
				//DG_NDLG[17, i].Value = grpk[i];
				//DG_NDLG[18, i].Value = dfrc[i];
			}
			
			TB_TFRI.Text = OTFRI;
			TB_TTFR.Text = OTTFR;

			DG_NDLG.Columns[ 0].HeaderText = MG.GRID_CL00;
			DG_NDLG.Columns[ 1].HeaderText = MG.GRID_CL01;
			DG_NDLG.Columns[ 2].HeaderText = MG.GRID_CL02;
			DG_NDLG.Columns[ 3].HeaderText = MG.GRID_CL03;
			DG_NDLG.Columns[ 4].HeaderText = MG.GRID_CL04;
			DG_NDLG.Columns[ 5].HeaderText = MG.GRID_CL05;
			DG_NDLG.Columns[ 6].HeaderText = MG.GRID_CL06;
			DG_NDLG.Columns[ 7].HeaderText = MG.GRID_CL07;
			DG_NDLG.Columns[ 8].HeaderText = MG.GRID_CL08;
			DG_NDLG.Columns[ 9].HeaderText = MG.GRID_CL09;
			DG_NDLG.Columns[10].HeaderText = MG.GRID_CL10;
			DG_NDLG.Columns[11].HeaderText = MG.GRID_CL11;

			//DG_NDLG.Columns[12].HeaderText = MG.GRID_CL12;
			//DG_NDLG.Columns[13].HeaderText = MG.GRID_CL13;
			//DG_NDLG.Columns[14].HeaderText = MG.GRID_CL14;
			//DG_NDLG.Columns[15].HeaderText = MG.GRID_CL15;
			//DG_NDLG.Columns[16].HeaderText = MG.GRID_CL16;
			//DG_NDLG.Columns[17].HeaderText = MG.GRID_CL17;
			//DG_NDLG.Columns[18].HeaderText = MG.GRID_CL18;
			
			CM.SetGrid( ( ( System.ComponentModel.ISupportInitialize )( this.DG_NDLG ) ) );
			CM.AlignCell( "Column0", "R" );
			CM.AlignCell( "Column1", "R" );
			CM.AlignCell( "Column2", "R" );
			CM.AlignCell( "Column3", "R" );
			CM.AlignCell( "Column4", "R" );
			CM.AlignCell( "Column5", "R" );
			CM.AlignCell( "Column6", "R" );
			CM.AlignCell( "Column7", "R" );
			CM.AlignCell( "Column8", "R" );
			CM.AlignCell( "Column9", "R" );
			CM.AlignCell( "Column10", "R" );
			//CM.AlignCell( "Column11", "R" );
		}
		
		public string OTFRI
		{
			get { return TFRI; }
			set { TFRI = value; }
		}

		public string OTTFR
		{
			get { return TTFR; }
			set { TTFR = value; }
		}

		private void BT_CLSE_Click( object sender, EventArgs e )
		{
			Close();
		}

		private void BT_EXCL_Click( object sender, EventArgs e )
		{	
			ExportExcel( true );
		}

		private void ExportExcel( bool captions ) 
		{
			this.FD_EXCL.FileName			= FileName;
			this.FD_EXCL.DefaultExt			= DefaultExt;
			this.FD_EXCL.Filter				= Filter;
			this.FD_EXCL.InitialDirectory	= Directory; 

			DialogResult result = FD_EXCL.ShowDialog();

			if (result == DialogResult.OK)
			{
				int num = 0;
				object missingType = Type.Missing;

				Excel.Application objApp;
				Excel._Workbook objBook;
				Excel.Workbooks objBooks;
				Excel.Sheets objSheets;
				Excel._Worksheet objSheet;
				Excel.Range range;

				string[] headers = new string[DG_NDLG.ColumnCount];
				string[] columns = new string[DG_NDLG.ColumnCount];

				for ( int c = 0; c < DG_NDLG.ColumnCount; c++ )
				{
					headers[c]	= DG_NDLG.Rows[0].Cells[c].OwningColumn.HeaderText.ToString();
					num			= c + 65;
					columns[c]	= Convert.ToString((char)num);
				}

				try
				{
					objApp		= new Excel.Application();
					objBooks	= objApp.Workbooks;
					objBook		= objBooks.Add(Missing.Value);
					objSheets	= objBook.Worksheets;
					objSheet	= (Excel._Worksheet)objSheets.get_Item(1);

					if (captions)
					{
						for ( int c = 0; c < DG_NDLG.ColumnCount; c++ )
						{
							range = objSheet.get_Range(columns[c] + "1", Missing.Value);
							range.set_Value(Missing.Value, headers[c]);
						}
					}

					for ( int r = 0; r < DG_NDLG.RowCount - 1; r++ )
					{
						for ( int c = 0; c < DG_NDLG.ColumnCount; c++ )
						{
							if ( DG_NDLG.Rows[r].Cells[c].Value == null )
								continue;

							range = objSheet.get_Range( columns[c] + Convert.ToString(r + 2), Missing.Value) ;
							range.set_Value( Missing.Value, DG_NDLG.Rows[r].Cells[c].Value.ToString() );
						}
					}

					objApp.Visible = false;
					objApp.UserControl = false;

					objBook.SaveAs( @FD_EXCL.FileName,
							  Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
							  missingType, missingType, missingType, missingType,
							  Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
							  missingType, missingType, missingType, missingType, missingType);
					objBook.Close( false, missingType, missingType );

					Cursor.Current = Cursors.Default;

					MessageBox.Show( MG.COMM_SAVE_SUCC );
				}
				catch ( Exception theException )
				{
					String errorMessage;
					errorMessage = "Error: ";
					errorMessage = String.Concat(errorMessage, theException.Message);
					errorMessage = String.Concat(errorMessage, " Line: ");
					errorMessage = String.Concat(errorMessage, theException.Source);

					MessageBox.Show(errorMessage, "Error");
				}
			}
		}
	}
}
